package contentservicelogic

import (
	"center-data-platform/app/content/rpc/content"
	"center-data-platform/app/content/rpc/internal/svc"
	"center-data-platform/app/user/rpc/user"
	"context"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *content.PostListReq) (*content.PostListResp, error) {
	res, err := l.svcCtx.ContentModel.List(l.svcCtx.DB, in)
	if err != nil {
		return nil, err
	}
	var result []*content.PostDetail
	for _, v := range res {
		userInfo, _ := l.svcCtx.UserRpc.Info(l.ctx, &user.UserInfoReq{Id: v.UserId})
		item := &content.PostDetail{
			Id:          v.Id,
			Title:       v.Title,
			Type:        v.Type,
			Content:     v.Content,
			Cover:       v.Cover,
			Description: v.Description,
			Tags:        strings.Split(v.Tags, ","),
			FileIds:     strings.Split(v.FileIds, ","),
			Status:      v.Status,
			UserId:      v.UserId,
			UserName:    userInfo.GetUserName(),
			CreatedAt:   v.CreatedTime.Format("2006-01-02 15:04:05"),
			UpdatedAt:   v.UpdatedTime.Format("2006-01-02 15:04:05"),
		}
		result = append(result, item)
	}
	return &content.PostListResp{
		Code: 0,
		Msg:  "请求成功",
		Data: result,
	}, nil
}
